package com.javaear.zblog4java.system.controller.manager;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.javaear.generalmapper.GeneralMapper;
import com.javaear.zblog4java.system.component.AppConfig;
import com.javaear.zblog4java.system.entity.model.CategoryModel;
import com.javaear.zblog4java.system.util.RequestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author aooer 2016/10/11.
 * @version 2.0.0
 * @since 2.0.0
 */
@Controller
@RequestMapping("system/management")
public class CategoryManagerController {

    private static final String MNG_URL = "redirect:%s-management.html";
    private static final String EDT_URL = "system/edit/%s-edit";
    private static final String ADD_URL = "system/add/%s-add";

    @Autowired
    private GeneralMapper generalMapper;

    @RequestMapping("category-management")
    public void list(HttpServletRequest request, ModelMap modelMap) {
        int pageNo = 1;
        String pageNoStr = request.getParameter("pageNo");
        if (StringUtils.isNotBlank(pageNoStr) && StringUtils.isNumeric(pageNoStr))
            pageNo = Integer.parseInt(pageNoStr);
        if (pageNo < 1)
            pageNo = 1;
        Page<CategoryModel> page = new Page<>(pageNo, (int) AppConfig.settingMap.get("App-systemPageSize"));
        modelMap.put("models", generalMapper.selectPage(page, new EntityWrapper<>(new CategoryModel())));
        modelMap.put("page", page);
        modelMap.put("title", "分类管理");
    }

    @RequestMapping("category-add")
    public String add(HttpServletRequest request, ModelMap modelMap) {
        String type = request.getParameter("type");
        CategoryModel categoryModel = RequestUtils.copyProperties(request, CategoryModel.class);
        if ((!StringUtils.isEmpty(type) && "show".equals(type)) || categoryModel == null) {
            modelMap.put("title", "新建分类");
            return String.format(ADD_URL, "category");
        }
        try {
            categoryModel.check();
        } catch (Exception e) {
            modelMap.put("title", "新建分类");
            return String.format(ADD_URL, "category");
        }
        return String.format(generalMapper.insertSelective(categoryModel) > 0 ? MNG_URL : ADD_URL, "category");
    }

    @RequestMapping("category-edit")
    public String edit(HttpServletRequest request, ModelMap modelMap) {
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id))
            return String.format(MNG_URL, "category");
        String type = request.getParameter("type");
        CategoryModel categoryModel = RequestUtils.copyProperties(request, CategoryModel.class);
        if ((StringUtils.isNotBlank(type) && "show".equals(type)) || categoryModel == null) {
            modelMap.put("model", generalMapper.selectById(Integer.parseInt(id), CategoryModel.class));
            modelMap.put("title", "编辑分类");
            return String.format(EDT_URL, "category");
        }
        return String.format(generalMapper.updateSelectiveById(categoryModel) > 0 ? MNG_URL : EDT_URL, "category");
    }

    @RequestMapping("category-remove")
    public String remove(HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id))
            generalMapper.deleteById(Integer.parseInt(id), CategoryModel.class);
        return String.format(MNG_URL, "category");
    }

}
